#include "categorylimitdialog.h"
#include "ui_categorylimitdialog.h"

CategoryLimitDialog::CategoryLimitDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CategoryLimitDialog)
{
    ui->setupUi(this);
}

CategoryLimitDialog::~CategoryLimitDialog()
{
    delete ui;
}

void CategoryLimitDialog::addCategory(const GameCategory &category, const QString &key) {
    Q_ASSERT_X(!keyIndex.contains(key), "CategoryLimitDialog::addCategory()", "Attempting to replace existing element");
    QTableWidgetItem *nameColumn = new QTableWidgetItem(category.getName());
    nameColumn->setData(Qt::UserRole, QVariant(key));
    nameColumn->setFlags(nameColumn->flags() & ~Qt::ItemIsEditable);
    QTableWidgetItem *numericColumn = new QTableWidgetItem("");
    QTableWidgetItem *pointsColumn = new QTableWidgetItem("");
    QTableWidgetItem *percentColumn = new QTableWidgetItem("");
    int row = ui->tableWidget->rowCount();
    ui->tableWidget->setRowCount(row + 1);
    ui->tableWidget->setItem(row, 0, nameColumn);
    ui->tableWidget->setItem(row, 1, numericColumn);
    ui->tableWidget->setItem(row, 2, pointsColumn);
    ui->tableWidget->setItem(row, 3, percentColumn);
    ui->tableWidget->setCurrentItem(nameColumn);
    keyIndex.insert(key, row);
}

void CategoryLimitDialog::updateCategory(const QString& oldKey, const GameCategory& newCategory,
                     const QString& newKey) {
    int row = keyIndex.value(oldKey, -1);
    if(row > -1) {
        ui->tableWidget->item(row, 0)->setText(newCategory.getName());
        if(oldKey != newKey) {
            ui->tableWidget->item(row, 0)->setData(Qt::UserRole, QVariant(newKey));
            keyIndex.remove(oldKey);
            keyIndex.insert(newKey, row);
        }
    }
}

void CategoryLimitDialog::removeCategory(const QString& key) {
    int row = keyIndex.value(key, -1);
    if(row > -1) {
        delete ui->tableWidget->item(row, 0);
        delete ui->tableWidget->item(row, 1);
        delete ui->tableWidget->item(row, 2);
        delete ui->tableWidget->item(row, 3);
        ui->tableWidget->removeRow(row);
        keyIndex.remove(key);

        // Rebuild index for modified items
        for(;row<ui->tableWidget->rowCount();++row) {
            keyIndex.insert(ui->tableWidget->item(row, 0)->data(Qt::UserRole).toString(),
                            row);
        }
    }
}

void CategoryLimitDialog::clearAll() {
    keyIndex.clear();
    ui->tableWidget->setRowCount(1);
}

CategoryDataHash CategoryLimitDialog::getItemLimits(const CategoryDataHash& currentValue) {
    CategoryDataHash::const_iterator x;
    for(x=currentValue.constBegin(); x != currentValue.constEnd(); ++x) {
        int index;
        if(x.key().isEmpty())
            index = 0;
        else
            index = keyIndex.value(x.key(), -1);
        if(index > -1) {
            const InheritedCategoryData &d(x.value());
            QString text;

            if(d.numericLimit != -1)
                text = QString::number(d.numericLimit);
            ui->tableWidget->item(index, 1)->setText(text);
            if(d.pointLimit != -1)
                text = QString::number(d.pointLimit);
            else
                text = "";
            ui->tableWidget->item(index, 2)->setText(text);
            if(d.percentageLimit != -1)
                text = QString::number(d.percentageLimit);
            else
                text = "";
            ui->tableWidget->item(index, 3)->setText(text);
        }
    }

    if(this->exec() == QDialog::Accepted) {
        CategoryDataHash value;
        for(int i = 0; i<ui->tableWidget->rowCount(); ++i) {
            InheritedCategoryData d;
            QString text = ui->tableWidget->item(i,1)->text();
            d.numericLimit = text.isEmpty() ? -1 : text.toUInt();
            text = ui->tableWidget->item(i,2)->text();
            d.pointLimit = text.isEmpty() ? -1 : text.toUInt();
            text = ui->tableWidget->item(i,3)->text();
            d.percentageLimit = text.isEmpty() ? -1 : text.toUInt();
            value.insert(ui->tableWidget->item(i, 0)->data(Qt::UserRole).toString(), d);
        }
        return value;
    }
    return currentValue;
}
